cmake_minimum_required(VERSION 2.8)
project(gdal)

option(GDAL_FRMTS_SUPPORTED "GDAL frmts supported")
option(GDAL_OGR_FRMTS_SUPPORTED "GDAL ogr frmts supported")
option(GDAL_ODBC_SUPPORTED "GDAL odbc frmts supported")

file(GLOB_RECURSE lib_hdrs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.h)
file(GLOB_RECURSE lib_srcs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c *.cpp)
file(GLOB lib2_srcs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} apps/*_lib.cpp)
list(APPEND lib2_srcs apps/commonutils.cpp)

set(CMAKE_DEBUG_POSTFIX "D")
set(exclude_dirs internal_libqhull
aaigrid
adrg
aigrid
airsar
arg
blx
bmp
bpg
cals
ceos
ceos2
coasp
cosar
ctg
dds
dimap
dods
dted
e00grid
ecw
elas
envisat
epsilon
ers
fit
fits
georaster
gff
grass
grib
gsg
gta
gxf
hdf4
hdf5
hf2
idrisi
ilwis
ingr
iris
iso8211
jaxapalsar
jdem
jp2kak
jp2lura
jpeg2000
jpegls
jpipkak
kea
kmlsuperoverlay
l1b
leveller
map
mbtiles
mrf
mrsid
mrsid_lidar
msg
msgn
netcdf
ngsgeoid
nitf
northwood
openjpeg
ozi
pcidsk
pcraster
pdf
pds
plmosaic
postgisraster
prf
r
rasdaman
rasterlite
rda
rik
rmf
rs2
safe
saga
sde
sdts
sentinel2
sgi
srtmhgt
terragen
til
tsx
usgsdem
wcs
webp
wms
wmts
xpm
xyz
zmap
aeronavfaa
amigocloud
arcgen
arcobjects
avc
bna
cad
carto
cloudant
couchdb
csv
csw
db2
dgn
dods
dwg
dxf
edigeo
elastic
filegdb
fme
geoconcept
geomedia
georss
gft
gml
gmlas
gmt
gpkg
gpsbabel
gpx
grass
gtm
htf
idb
idrisi
ili
ingres
jml
libkml
mdb
mongodb
mssqlspatial
mvt
mysql
nas
ntf
oci
odbc
ods
ogdi
openair
openfilegdb
osm
pds
pg
pgdump
pgeo
plscenes
rec
s57
sde
sdts
segukooa
segy
selafin
sosi
sqlite
sua
sxf
tiger
vdv
vfk
walk
wasp
wfs
xls
xlsx
xplane
)
set(exclude_file vsipreload.cpp generate_encoding_table.c shpopen.c dbfopen.c shptree.c sbnsearch.c gdal_nrgcrs.c cpl_odbc.cpp kml.cpp kmlnode.cpp kmlvector.cpp)

if(GDAL_FRMTS_SUPPORTED)
  add_definitions(-DFRMT_ceos -DFRMT_aigrid -DFRMT_elas -DFRMT_hfa -DFRMT_sdts -DFRMT_raw -DFRMT_gxf -DFRMT_ceos2 -DFRMT_dted -DFRMT_jdem -DFRMT_envisat -DFRMT_aaigrid -DFRMT_usgsdem -DFRMT_l1b -DFRMT_fit -DFRMT_xpm -DFRMT_bmp -DFRMT_rmf -DFRMT_nitf -DFRMT_pcidsk -DFRMT_airsar -DFRMT_rs2 -DFRMT_ilwis -DFRMT_msgn -DFRMT_rik -DFRMT_pcraster -DFRMT_leveller -DFRMT_sgi -DFRMT_srtmhgt -DFRMT_idrisi -DFRMT_jaxapalsar -DFRMT_ers -DFRMT_ingr -DFRMT_dimap -DFRMT_gff -DFRMT_terragen -DFRMT_gsg -DFRMT_cosar -DFRMT_pds -DFRMT_adrg -DFRMT_coasp -DFRMT_tsx -DFRMT_blx -DFRMT_til -DFRMT_r -DFRMT_northwood -DFRMT_saga -DFRMT_xyz -DFRMT_hf2 -DFRMT_kmlsuperoverlay -DFRMT_ozi -DFRMT_ctg -DFRMT_e00grid -DFRMT_zmap -DFRMT_ngsgeoid -DFRMT_iris -DFRMT_map -DFRMT_cals -DFRMT_safe -DFRMT_sentinel2 -DFRMT_derived -DFRMT_prf)
  #include_directories(ogr/ogrsf_frmts/cad/libopencad frmts/sdts frmts/pds frmts/idrisi ogr/ogrsf_frmts/avc)
  list(REMOVE_ITEM exclude_dirs ceos aigrid elas hfa sdts raw gxf ceos2 dted jdem envisat aaigrid usgsdem l1b fit xpm bmp rmf nitf pcidsk airsar rs2 ilwis msgn rik pcraster leveller sgi srtmhgt idrisi jaxapalsar ers ingr dimap gff terragen gsg cosar pds adrg coasp tsx blx til r northwood saga xyz hf2 kmlsuperoverlay ozi ctg e00grid zmap ngsgeoid iris map cals safe sentinel2 derived prf)
  #list(APPEND exclude_file ntfdump.cpp s57dump.cpp dgndump.cpp bsb2raw.c sdts2shp.cpp 8211createfromxml.cpp 8211dump.cpp 8211view.cpp mkcatalog.cpp)
endif()

if(GDAL_OGR_FRMTS_SUPPORTED)
  add_definitions(-DCSV_ENABLED -DNTF_ENABLED -DSDTS_ENABLED -DTIGER_ENABLED -DS57_ENABLED -DDGN_ENABLED -DAVCBIN_ENABLED -DREC_ENABLED -DGML_ENABLED -DGMT_ENABLED -DBNA_ENABLED -DKML_ENABLED -DGPX_ENABLED -DGEOCONCEPT_ENABLED -DXPLANE_ENABLED -DGEORSS_ENABLED -DGTM_ENABLED -DDXF_ENABLED -DPGDUMP_ENABLED -DGPSBABEL_ENABLED -DSUA_ENABLED -DOPENAIR_ENABLED -DPDS_ENABLED -DHTF_ENABLED -DAERONAVFAA_ENABLED -DEDIGEO_ENABLED -DIDRISI_ENABLED -DARCGEN_ENABLED -DSEGUKOOA_ENABLED -DSEGY_ENABLED -DSXF_ENABLED -DOPENFILEGDB_ENABLED -DWASP_ENABLED -DSELAFIN_ENABLED -DJML_ENABLED -DVDV_ENABLED -DCAD_ENABLED -DOCAD_EXTERN=)
  include_directories(ogr/ogrsf_frmts/cad/libopencad frmts/sdts frmts/pds frmts/idrisi ogr/ogrsf_frmts/avc)
  list(REMOVE_ITEM exclude_dirs idrisi iso8211 pds sdts aeronavfaa arcgen avc bna cad csv dgn dxf edigeo geoconcept georss gml gmt gpsbabel gpx gtm htf idrisi jml ntf openair openfilegdb pds pgdump rec s57 sdts segukooa segy selafin sua svg sxf tiger vdv wasp xplane)
  list(APPEND exclude_file ntfdump.cpp s57dump.cpp dgndump.cpp bsb2raw.c sdts2shp.cpp 8211createfromxml.cpp 8211dump.cpp 8211view.cpp mkcatalog.cpp)
endif()

if(HDF5_FOUND)
  add_definitions(/DHAVE_HDF5 -DHDF5_PLUGIN=NO -DFRMT_hdf5 -DWIN32 -DH5_BUILT_AS_DYNAMIC_LIB)
  include_directories(${HDF5_INCLUDE_DIRS} ${HDF5_SOURCE_DIR}/src)
  list(APPEND lib_libs ${HDF5_LIBRARIES})
  list(REMOVE_ITEM exclude_dirs hdf5)
endif()
if(NETCDF_FOUND)
  add_definitions(/DHAVE_NETCDF -DNETCDF_PLUGIN=NO -DNETCDF_SETTING=yes -DFRMT_netcdf)
  include_directories(${NETCDF_INCLUDE_DIRS} ${NETCDF_SOURCE_DIR}/include)
  list(APPEND lib_libs ${NETCDF_LIBRARIES})
  list(REMOVE_ITEM exclude_dirs netcdf)
endif()
if(LIBCURL_FOUND)
  add_definitions(/DHAVE_CURL -DFRMT_wms -DFRMT_plmosaic -DFRMT_wmts -DFRMT_rda -DWFS_ENABLED -DCSW_ENABLED -DGFT_ENABLED -DCOUCHDB_ENABLED -DCLOUDANT_ENABLED -DELASTIC_ENABLED -DCARTO_ENABLED -DAMIGOCLOUD_ENABLED)
  include_directories(ogr/ogrsf_frmts/gml ogr/ogrsf_frmts/pgdump ogr/ogrsf_frmts/wfs ogr/ogrsf_frmts/couchdb ${LIBCURL_INCLUDE_DIRS} ${LIBSSH2_INCLUDE_DIR} ${LIBCURL_SOURCE_DIR}/include ${LIBSSH2_SOURCE_DIR}/include)
  list(APPEND lib_libs ${LIBCURL_LIBRARIES})
  list(REMOVE_ITEM exclude_dirs wms plmosaic wmts rda   gml pgdump wfs gft cloudant couchdb elastic carto amigocloud csw)
endif()

if(ICONV_FOUND)
  add_definitions(/DHAVE_ICONV -DICONV_CONST=const)
  include_directories(${ICONV_INCLUDE_DIRS} ${OPENSSL_SOURCE_DIR}/../include)
  list(APPEND lib_libs ${ICONV_LIBRARIES})
endif()
#if(EXPAT_FOUND)
#  add_definitions(/DHAVE_EXPAT /DXLSX_ENABLED)
#  include_directories(${EXPAT_INCLUDE_DIRS})
#  list(APPEND lib_libs ${EXPAT_LIBRARIES})
#  list(REMOVE_ITEM exclude_dirs xlsx)
#endif()
if(GEOS_FOUND)
  add_definitions(/DHAVE_GEOS)
  include_directories(${GEOS_INCLUDE_DIRS} ${GEOS_SOURCE_DIR}/capi ${GEOS_SOURCE_DIR}/include)
  list(APPEND lib_libs ${GEOS_LIBRARIES})
endif()
if(LIBLZMA_FOUND)
  add_definitions(/DLZMA_SUPPORT)
  include_directories(${LIBLZMA_INCLUDE_DIRS} ${LIBLZMA_SOURCE_DIR}/api)
  list(APPEND lib_libs ${LIBLZMA_LIBRARIES})
endif()
if(LIBZSTD_FOUND)
  add_definitions(/DZSTD_SUPPORT)
  include_directories(${LIBZSTD_INCLUDE_DIRS})
  list(APPEND lib_libs ${LIBZSTD_LIBRARIES})
endif()
if(LIBXML2_FOUND)
  add_definitions(/DHAVE_LIBXML2)
  include_directories(${LIBXML2_INCLUDE_DIRS} ${LIBXML2_SOURCE_DIR}/include)
  list(APPEND lib_libs ${LIBXML2_LIBRARIES})
endif()
if(FREEXL_FOUND)
  add_definitions(/DFREEXL_ENABLED)
  include_directories(${FREEXL_INCLUDE_DIRS})
  list(APPEND lib_libs ${FREEXL_LIBRARIES})
  list(REMOVE_ITEM exclude_dirs xls)
endif()
if(OPENSSL_FOUND)
  add_definitions(/DHAVE_OPENSSL_CRYPTO)
  include_directories(${OPENSSL_INCLUDE_DIRS} ${OPENSSL_SOURCE_DIR}/../include)
  list(APPEND lib_libs ${OPENSSL_LIBRARIES})
endif()
if(SQLITE3_FOUND)
  message(STATUS "include:${SQLITE3_INCLUDE_DIRS}" " lib:${SQLITE3_LIBRARIES}")
  add_definitions(/DHAVE_SQLITE -DSQLITE_HAS_COLUMN_METADATA -DSQLITE_ENABLED -DOSM_ENABLED -DVFK_ENABLED -DMVT_ENABLED -DFRMT_mbtiles -DFRMT_rasterlite)
  include_directories(${SQLITE3_INCLUDE_DIRS} ogr/ogrsf_frmts/sqlite ogr/ogrsf_frmts/gpkg ogr/ogrsf_frmts/mvt ogr/ogrsf_frmts/osm ogr/ogrsf_frmts/vfk)
  list(APPEND lib_libs ${SQLITE3_LIBRARIES})
  list(REMOVE_ITEM exclude_dirs sqlite gpkg rasterlite mbtiles mvt osm vfk)
  list(APPEND exclude_file ogrsqliteregexp.cpp ogrsqlitesqlfunctions.cpp osm2osm.cpp xmlreformat.cpp)
endif()
if(SPATIALITE_FOUND)
  add_definitions(/DHAVE_SPATIALITE -DSPATIALITE_412_OR_LATER)
  include_directories(${SPATIALITE_INCLUDE_DIRS} ${SPATIALITE_SOURCE_DIR}/headers)
  list(APPEND lib_libs ${SPATIALITE_LIBRARIES})
endif()
if(GDAL_ODBC_SUPPORTED)
  add_definitions(-DODBC_ENABLED -DPGEO_ENABLED -DDB2_ENABLED -DMSSQLSPATIAL_ENABLED -DGEOMEDIA_ENABLED -DWALK_ENABLED)
  include_directories(ogr/ogrsf_frmts/pgeo)
  list(APPEND lib_libs legacy_stdio_definitions.lib odbc32.lib odbccp32.lib)
  list(REMOVE_ITEM exclude_dirs odbc pgeo mssqlspatial geomedia db2 walk)
  list(REMOVE_ITEM exclude_file cpl_odbc.cpp)
  list(APPEND exclude_file ogrdb2geometryvalidator.cpp)
endif()
#message(STATUS "b:${exclude_dirs}")

set(reg_dirs "ci/.*|swig/.*|apps/.*|fuzzers/.*")
set(reg_file ".*test.*")
macro(add_regex_dirs)
  foreach(_var ${ARGN})
    string(APPEND reg_dirs "|.*/")
    string(APPEND reg_dirs ${_var})
    string(APPEND reg_dirs "/.*")
  endforeach()
endmacro()
macro(add_regex_file)
  foreach(_var ${ARGN})
    string(APPEND reg_file "|.*")
    string(APPEND reg_file ${_var})
  endforeach()
endmacro()
add_regex_dirs(${exclude_dirs})
add_regex_file(${exclude_file})

list(FILTER lib_hdrs EXCLUDE REGEX ${reg_dirs})
list(FILTER lib_srcs EXCLUDE REGEX ${reg_dirs})
list(FILTER lib_srcs EXCLUDE REGEX ${reg_file})
include_directories(../apps alg frmts gcore gnm port ogr ogr/ogrsf_frmts gnm/gnm_frmts frmts/vrt frmts/iso8211 frmts/raw frmts/zlib frmts/png/libpng frmts/gif/giflib frmts/jpeg/libjpeg frmts/mem frmts/gtiff frmts/gtiff/libgeotiff frmts/gtiff/libtiff ogr/ogrsf_frmts/generic ogr/ogrsf_frmts/shape ogr/ogrsf_frmts/mem ogr/ogrsf_frmts/geojson ogr/ogrsf_frmts/geojson/libjson)
add_definitions(-DNOMINMAX -DINTERNAL_LIBGEOTIFF /DWIN64 /DHAVE_LIBZ /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE /DGDAL_COMPILATION /DSHAPELIB_DLLEXPORT -DUSE_CPL -DSAOffset=vsi_l_offset -DJPEG12_SUPPORTED=1 -DJPEG_DUAL_MODE_8_12 -DDEFAULT_MAX_MEM=500000000L -DJPEG_SUPPORT -DOJPEG_SUPPORT -DPAM_ENABLED)
add_definitions(-DSHAPE_ENABLED -DMEM_ENABLED -DVRT_ENABLED -DTAB_ENABLED -DGEOJSON_ENABLED -DSVG_ENABLED -DGNM_ENABLED -DFRMT_jpeg -DFRMT_png -DFRMT_gtiff -DFRMT_gif -DFRMT_shape -DFRMT_mem -DFRMT_vrt -DFRMT_tab -DFRMT_geojson -DFRMT_svg -DFRMT_null)

add_library(${PROJECT_NAME} SHARED ${lib_srcs} ${lib2_srcs} ${lib_hdrs})
target_link_libraries(${PROJECT_NAME} ${lib_libs} ws2_32.lib)
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}-targets RUNTIME DESTINATION bin ARCHIVE DESTINATION lib)
install(EXPORT ${PROJECT_NAME}-targets DESTINATION lib/cmake FILE ${PROJECT_NAME}-config.cmake)

if(BUILD_STATIC)
  add_library(${PROJECT_NAME}S STATIC ${lib_srcs} ${lib2_srcs} ${lib_hdrs})
  install(TARGETS ${PROJECT_NAME}S EXPORT ${PROJECT_NAME}S-targets RUNTIME DESTINATION bin ARCHIVE DESTINATION lib)
  install(EXPORT ${PROJECT_NAME}S-targets DESTINATION lib/cmake FILE ${PROJECT_NAME}S-config.cmake)
endif()


if(BUILD_APPS)
  add_subdirectory(apps)
endif()

unset(lib_hdrs)
macro(add_hdrs)
  foreach(_var ${ARGN})
    file(GLOB inc_hdrs ${_var}/*.h)
    list(APPEND lib_hdrs ${inc_hdrs})
  endforeach()
endmacro()

add_hdrs(port gcore alg ogr gnm frmts/vrt ogr/ogrsf_frmts)
list(APPEND lib_hdrs frmts/mem/memdataset.h frmts/raw/rawdataset.h apps/gdal_utils.h)
install(FILES ${lib_hdrs} DESTINATION include/gdal)
install(DIRECTORY data/ DESTINATION share/gdal)